//
//  LoginViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/24.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "LoginViewController.h"
#import <SMS_SDK/SMSSDK.h>
@interface LoginViewController (){
    NSInteger count;
}



@property(nonatomic, retain)UITextField *yanzhengText;
@property(nonatomic, retain)UITextField *textfield;

@property(nonatomic, retain)UIButton *yanzheng;
@property(nonatomic, retain)UIButton *ok;
@property(nonatomic,retain)NSTimer *timer;
@end

@implementation LoginViewController

- (void)dealloc
{
    [_textfield release];
    [_yanzhengText release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    self.navigationItem.title = @"短信验证";
    
    //重写左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    

    UIImageView  *img = [[ UIImageView alloc] initWithFrame:self.view.frame];
    img.image = [ UIImage imageNamed:@"loginBackgroundImage"];
    [self.view addSubview:img];
    [img release];
    /**手机号码输入框*/
  self.textfield = [[ UITextField alloc] initWithFrame:CGRectMake(60 * WIDTH, 150 *HEIGHT, 200 * WIDTH, 40 * HEIGHT)];
    self.textfield.placeholder = @"请输入手机号码";
    self.textfield.backgroundColor = [UIColor whiteColor];
    self.textfield.textAlignment = NSTextAlignmentCenter;
    self.textfield .layer.masksToBounds = YES;
    self.textfield.layer.cornerRadius = 10;
    [self.view addSubview:self.textfield];
    [_textfield release];
    /**验证码按钮*/
   self.yanzheng =[ UIButton buttonWithType:UIButtonTypeSystem];
    [self.yanzheng setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.yanzheng setTitleColor:[ UIColor blackColor] forState:UIControlStateNormal];
    self.yanzheng.frame = CGRectMake(160 * WIDTH, 200 *HEIGHT, 100 * WIDTH, 40 *HEIGHT);
    [self.yanzheng addTarget:self action:@selector(yanzhengAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.yanzheng];
    /**验证码输入框*/
    self.yanzhengText =[[ UITextField alloc] initWithFrame:CGRectMake(60 *WIDTH, 200 *HEIGHT, 100 * WIDTH, HEIGHT * 40)];
    self.yanzhengText.placeholder = @"输入验证码";
    self.yanzhengText.backgroundColor = [UIColor whiteColor];
    self.yanzhengText.layer.cornerRadius = 10;
    self.yanzhengText.layer.masksToBounds = YES;
    [self.view addSubview:self.yanzhengText];
    [_yanzhengText release];
    /**确定按钮*/
    self.ok =[ UIButton buttonWithType:UIButtonTypeSystem];
    [self.ok setTitle:@"确定" forState:UIControlStateNormal];
    [self.ok setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.ok.frame = CGRectMake(60 * WIDTH, 270 * HEIGHT, 200 * WIDTH, 50 * HEIGHT);
    self.ok.backgroundColor = [ UIColor orangeColor];
    [self.ok addTarget:self action:@selector(okAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.ok];
    
}
#pragma mark -点击验证码方法
-(void)yanzhengAction
{
    /**
     *  @from                    v1.1.1
     *  @brief                   获取验证码(Get verification code)
     *
     *  @param method            获取验证码的方法(The method of getting verificationCode)
     *  @param phoneNumber       电话号码(The phone number)
     *  @param zone              区域号，不要加"+"号(Area code)
     *  @param customIdentifier  自定义短信模板标识 该标识需从官网http://www.mob.com上申请，审核通过后获得。(Custom model of SMS.  The identifier can get it  from http://www.mob.com  when the application had approved)
     *  @param result            请求结果回调(Results of the request)
     */
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_textfield.text zone:@"86"customIdentifier:nil result:^(NSError *error){
        if (!error) {
            NSLog(@"获取验证码成功");
        [self performSelector:@selector(countAction) withObject:nil];
            
        }
        else {
            NSLog(@"错误信息：%@",error);
        }
    }];
    
}
#pragma mark -确定按钮点击方法
-(void)okAction
{
    [SMSSDK commitVerificationCode:self.yanzhengText.text phoneNumber:self.textfield.text zone:@"86" result:^(NSError *error) {
        
        if (!error) {
            NSLog(@"验证成功");
            UIAlertController *c = [UIAlertController alertControllerWithTitle:@"提示" message:@"短信验证成功" preferredStyle:UIAlertControllerStyleAlert];
            
            [self presentViewController:c animated:YES completion:^{
                [self.navigationController popViewControllerAnimated:YES];
                           }];
            //延时消失
            [self performSelector:@selector(diss) withObject:self afterDelay:2];
            
            

            
        }
        else
        {
            NSLog(@"错误信息:%@",error);
        }
    }];
}
#pragma mark -倒计时方法
-(void)countAction
{
    _yanzheng.enabled =NO;
    count = 60;
    [_yanzheng setTitle:@"60秒" forState:UIControlStateDisabled];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

-(void)timerFired:(NSTimer *)timer
{
    if (count !=1) {
        count -=1;
        [_yanzheng setTitle:[NSString stringWithFormat:@"%ld秒",(long)count] forState:UIControlStateDisabled];
    }
    else
    {
        [_timer invalidate];
        _yanzheng.enabled = YES;
        [_yanzheng setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}
-(void)leftAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)diss
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
